import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Panel;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JLabel;

public class Login {

	private JTextField txtF_ID; // ID
	private JPasswordField txtF_Pass; // password
	private JTextField txtId;
	private JTextField txtPassword;
	private final JTextField txtWelcome = new JTextField();
	JTextField txtid;
	private CardLayout cardLayoutLogin;
	private JPanel cardLoginPanel;
	public MainPanel mp1 = new MainPanel();
	private String InputID = null;
	private String InputPW = null;
	private JButton btnLogout;

	public String getInputID() {
		String id = txtid.getText();

		return id;
	}

	public Login() {

		cardLoginPanel = new JPanel();
		cardLoginPanel.setBackground(Color.WHITE);

		final JPanel loginPanel = new JPanel();

		cardLayoutLogin = new CardLayout();
		cardLoginPanel.setLayout(cardLayoutLogin);
		cardLoginPanel.add(loginPanel);

		// card1 loginPanel
		cardLoginPanel.add(loginPanel, "name_2642457313697");
		loginPanel.setLayout(new GridLayout(3, 1, 0, 0));

		txtId = new JTextField();
		txtId.setBackground(new Color(127, 255, 212));
		txtId.setFont(new Font("Consolas", Font.PLAIN, 17));
		txtId.setHorizontalAlignment(SwingConstants.CENTER);
		txtId.setText("ID");
		txtId.setEditable(false);
		loginPanel.add(txtId);
		txtId.setColumns(10);

		txtF_ID = new JTextField();
		txtF_ID.setBackground(new Color(224, 255, 255));
		loginPanel.add(txtF_ID);
		txtF_ID.setColumns(10);

		txtPassword = new JTextField();
		txtPassword.setBackground(new Color(127, 255, 212));
		txtPassword.setFont(new Font("Consolas", Font.PLAIN, 17));
		txtPassword.setHorizontalAlignment(SwingConstants.CENTER);
		txtPassword.setEditable(false);
		txtPassword.setText("Password");
		loginPanel.add(txtPassword);
		txtPassword.setColumns(10);

		txtF_Pass = new JPasswordField();
		txtF_Pass.setBackground(new Color(224, 255, 255));
		loginPanel.add(txtF_Pass);

		JButton btnLogin = new JButton("Login");
		btnLogin.setFont(new Font("Consolas", Font.BOLD, 17));
		btnLogin.setBackground(SystemColor.inactiveCaption);
		loginPanel.add(btnLogin);

		JButton btnRegister = new JButton("Register");
		btnRegister.setFont(new Font("Consolas", Font.BOLD, 17));
		btnRegister.setBackground(SystemColor.inactiveCaption);
		loginPanel.add(btnRegister);

		JPanel loginSuccPanel = new JPanel();
		cardLoginPanel.add(loginSuccPanel);
		//---
		// card2 loginSuccPanel
		cardLoginPanel.add(loginSuccPanel, "name_2652003085285");
		loginSuccPanel.setLayout(new GridLayout(0, 1, 0, 0));
		txtWelcome.setEditable(false);
		txtWelcome.setBackground(new Color(224, 255, 255));
		txtWelcome.setFont(new Font("Yu Mincho Demibold", Font.PLAIN, 17));
		txtWelcome.setHorizontalAlignment(SwingConstants.CENTER);
		txtWelcome.setText("Welcome");
		loginSuccPanel.add(txtWelcome);
		txtWelcome.setColumns(10);
		
		//this is txt of id for the next panel after success login
		txtid = new JTextField();
		txtid.setHorizontalAlignment(SwingConstants.CENTER);
		txtid.setFont(new Font("Khmer UI", Font.BOLD, 16));
		txtid.setBackground(new Color(127, 255, 212));
		txtid.setEditable(false);
		
		// txtid.setText(txtF_ID.getText());
		loginSuccPanel.add(txtid);
		txtid.setColumns(10);

		btnLogout = new JButton("Logout");
		btnLogout.setBackground(SystemColor.inactiveCaption);
		btnLogout.setFont(new Font("Yu Mincho Demibold", Font.PLAIN, 17));
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				mp1.cardLayoutTicket.show(mp1.cardTicketPanel, "welcome");
				txtF_ID.setText("");
				txtF_Pass.setText("");

				cardLayoutLogin.next(cardLoginPanel);

				FileWriter currentIDWriter = null;
				try {
					currentIDWriter = new FileWriter("currentID.txt");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					currentIDWriter.write("");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					currentIDWriter.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
		loginSuccPanel.add(btnLogout);
		//It check the all exceptions 
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				if (txtF_ID.getText().equals("")
						|| txtF_Pass.getText().equals("")) {
					JOptionPane.showMessageDialog(loginPanel,
							"Enter ID or PW!", "Error!",
							JOptionPane.ERROR_MESSAGE);
				}

				else
					try {
						InputID = txtF_ID.getText();
						InputPW = txtF_Pass.getText();

						TextManager IDListmanager = new TextManager("IDList");
						ArrayList<String> IDList = new ArrayList<String>();
						IDList = IDListmanager.getTextList();// ArrayList

						int j = 0;
						Boolean idexist = false;
						outer: for (int i = 0; i < IDList.size(); i++) {
							if (InputID.equals(IDList.get(i))) {
								idexist = true;
								j = i;
								break outer;
							}

						}

						TextManager IDInfomanager = new TextManager(IDList
								.get(j));
						ArrayList<String> IDInfo = new ArrayList<String>();
						IDInfo = IDInfomanager.getTextList();
						
						//if id is exist and password is correct, it shows the next panel 
						if (idexist.equals(true)
								&& InputPW.equals(IDInfo.get(0))) {
							cardLayoutLogin.next(cardLoginPanel);

							FileWriter currentIDWriter = new FileWriter(
									"currentID.txt");
							currentIDWriter.write(InputID);
							currentIDWriter.close();
						} 
						//or error
						else {
							JOptionPane.showMessageDialog(loginPanel,
									"ID doesnt exist or wrong PW!", "Error!",
									JOptionPane.ERROR_MESSAGE);
						}
					}

					catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}// Read ID.txt for its contents.

				txtid.setText(InputID);

			}
		});
		
		//it calls RegisterPanel
		btnRegister.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				RegisterPanel rp = new RegisterPanel();
				rp.setTitle("Register");
				rp.setBounds(0, 0, 500, 300);
				rp.setVisible(true);

			}
		});
		//set the txtid as currentID. This is for the next card after login to show the user id
		try {
			FileReader currentIDReader = new FileReader("currentID.txt");
			BufferedReader currentMatchBufferedReader = new BufferedReader(
					currentIDReader);
			String currentID = null;
			String line = null;
			if ((line = currentMatchBufferedReader.readLine()) != null) {
				StringTokenizer st = new StringTokenizer(line, "-");
				currentID = st.nextToken();
				cardLayoutLogin.next(cardLoginPanel);
				txtid.setText(currentID);
			}
		} catch (IOException e1) {

		}

	}

	public JPanel login() {
		return cardLoginPanel;

	}

}
